package com.secondhand.controller;

import com.secondhand.domain.good;
import com.secondhand.domain.user;
import com.secondhand.service.Impl.goodServiceImpl;
import com.secondhand.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("二手交易相关接口")
@Log4j2
@RestController
@RequestMapping("/goods")
public class goodController {
    @Autowired
    goodServiceImpl goodService;

    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public AjaxResult getSiteList() {
        List<good> result = goodService.gainGoodsList();
        return AjaxResult.success("获取商品列表",result);
    }

    @ApiOperation("获取某重类型的商品列表")
    @GetMapping("/listbytypeid")
    @ApiImplicitParam(name = "typeid", value = "商品类型", required = true, dataType = "int")
    public AjaxResult getGoodByTypeId(int typeid) {
        List<good> result = goodService.getGoodByTypeId(typeid);
        return AjaxResult.success("获取商品列表",result);
    }

    @ApiOperation("添加商品")
    @PostMapping("/add")
    public AjaxResult register(@RequestBody good good) {
        boolean result = goodService.addGood(good);
        return AjaxResult.success("添加商品成功",result);
    }

    @ApiOperation("删除商品")
    @ApiImplicitParam(name = "goodid", value = "商品id", required = true, dataType = "int")
    @GetMapping("/delete")
    public AjaxResult logout(@RequestParam("goodid") int goodid){
        Boolean result = goodService.delGood(goodid);
        return AjaxResult.success("删除商品",result);
    }

    @ApiOperation("获取已卖出商品")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/sold")
    public AjaxResult gainSoldGoodsList(@RequestParam("userId") int userId) {
        List<good> result = goodService.gainSoldGoodsList(userId);
        return AjaxResult.success("已卖出商品",result);
    }

    @ApiOperation("获取已拍下商品")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/solding")
    public AjaxResult gainSoldingGoodsList(@RequestParam("userId") int userId) {
        List<good> result = goodService.gainSoldingGoodsList(userId);
        return AjaxResult.success("已拍下商品",result);
    }

    @ApiOperation("获取未卖出商品")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int")
    @GetMapping("/unsold")
    public AjaxResult gainUnSoldGoodsList(@RequestParam("userId") int userId) {
        List<good> result = goodService.gainUnSoldGoodsList(userId);
        return AjaxResult.success("未卖出商品",result);
    }

    @ApiOperation("商品信息修改")
    @PostMapping("/update")
    public AjaxResult updateGoodInfo(@RequestBody good good) {
        int result = goodService.updateGood(good);
        return AjaxResult.success("用户信息修改",result);
    }
}
